<?php


namespace app\admin\model;


use app\admin\validate\OrderDepositValidate;
use think\Db;
use think\Validate;

class OrderDepositSendModel extends BaseModel
{
    protected $name='order_deposit_send';

    protected $pk='id';

    protected $autoWriteTimestamp = true;
    protected $updateTime = false;
    protected $createTime = 'create_time';

    public function doAddData($data = [],$mw=[]){

        $validate = new OrderDepositValidate();
        if(!$validate->scene('edit_jc')->check($data)) {
            $this->error = $validate->getError();
            return false;
        }
        $this->startTrans();
        try {
            $deposit_id = $data['deposit_id'];
            $storage_time = $data['storage_time'];
            $ad_info = ArchivesDeceasedModel::where(['deposit_id' => $deposit_id])->find();
            $od_info = OrderDepositModel::where(['id' => $deposit_id])->find();
            $extension_time = unixtime_to_date_inc($od_info['deposit_end_time'], $storage_time);
//            OrderDepositModel::update(['deposit_end_time'=> unixtime_to_date_inc($od_info['deposit_end_time'],$storage_time)],['id'=>$deposit_id]);

            $xf['type'] = 1;
            $xf['deposit_id'] = $data['deposit_id'];
            $xf['customer_id'] = $mw['customer_id'];
            $xf['order_id'] = $mw['order_id'];
            $xf['mw_id'] = $mw['tomb_id'];
            $xf['mw_num'] = $mw['tomb_num'];
            $xf['buyer_name'] = $data['buyer_name'];
            $xf['buyer_sex'] = $data['buyer_sex'];
            $xf['buyer_phone'] = $data['buyer_phone'];
            $xf['relationship'] = $data['relationship'];
            $xf['cart_type'] = $data['cart_type'];
            $xf['cart_num'] = $data['cart_num'];
            $xf['cart_address'] = $data['cart_address'];
            $xf['deposit_cost'] = $data['deposit_cost'];
            $xf['storage_time'] = $storage_time;
            $xf['original_price'] = (1 - $data['original_price']) * 100;
            $xf['total_price'] = $data['total_price'];
            $xf['actual_amount'] = $data['actual_amount'];
            $xf['extension_time'] = $extension_time;
            $xf['deceased_id'] = $ad_info['deceased_id'];

            $out_info = $this->create($xf);
            $other_id = $out_info['id'];
            $pay_ins = [
                'type' => 3,
                'itype' => 5,
                'pay_sn' => OrderPayLogModel::getselfPayNum('add_time', 'pay_sn', 'JCXF'),//成交
                'tomb_id' => $mw['tomb_id'],
                'tomb_num' => $mw['tomb_num'],
                'tom_price' => $data['deposit_cost'],
                'free_price' => $data['original_price'],
                'deal_price' => $data['actual_amount'],//成交价
                'total_price' => $data['actual_amount'],//应收金额
                'actual_amount' => $data['total_price'],//实际收费
                'real_name' => $mw['buyer_name'],//付款人
                'real_phone' => $mw['buyer_phone'],//付款人
                'other_id' => $other_id,//付款人
                'handle' => cmf_get_current_admin_id(),
                'pay_type' => 1,
                'add_time' => time(),
                'collect_time' => time(),
                'status' => 0,
                'customer_id' => $mw['customer_id'],
            ];
            $pay_id = Db::name('order_pay_log')->insertGetId($pay_ins);
            $this->update(['pay_id' => $pay_id], ['id' => $other_id]);
            $this->commit();
            return true;
        }catch(\Exception $e) {
            $this->error = '操作失败';
            $this->rollback();
            return false;
        }
    }

}